/*
 * @Author: GuitarXiaohe && 2205227953@qq.com
 * @Date: 2024-01-13 17:33:58
 * @LastEditors: 18163104359 2205227953@qq.com
 * @LastEditTime: 2024-01-26 17:42:27
 * @FilePath: /umi/src/app.jsx
 * @Description:app入口 相当于 main.js
 * QQ:2205227953,邮箱:2205227953@qq.com
 * Copyright (c) 2024 by GuitarXiaohe, All Rights Reserved.
 */

/**
 * 这里需要了解生命周期 app 最先执行 很多api在这里用不了 请注意！！！！
 */
import React from "react";
import { GetRoutersAPI } from "@/api/common";
import { getToken } from "./utils/auth";
import { parseRoutes } from "./utils/index";
import "@/assets/styles/index.less";
import "@/assets/iconAli/iconfont.css";
let extraRoutes = [];

const pathComponent = (component) => {
  return new Promise((resolve, reject) => {
    import(component)
      .then((module) => {
        resolve(module);
      })
      .catch((error) => {
        console.log("页面未找到文件", error);
        resolve(import(`@/pages/404`));
      });
  });
};

// 执行方法 遍历路由
export async function patchClientRoutes({ routes }) {
  const routerIndex = routes.findIndex((item) => item.path === "/");
  // 存在 & 成功获取动态路由数据
  if (extraRoutes) {
    const r = parseRoutes(extraRoutes);

    routes[routerIndex]["routes"].push(...r);
    console.log(routes);
  }
}

// 周期
export async function render(oldRender) {
  // 判断失去token 不在获取路由
  if (getToken()) {
    const { data: result } = await GetRoutersAPI();

    // let arr = [];
    // result.map((t) => {
    //   if (t.menuType == "C" && t.parentId == "0" && t.children) {
    //     arr.push(...t.children);
    //   } else {
    //     if (t.menuType == "M") {
    //       return arr.push(t);
    //     }
    //   }
    // });

    extraRoutes = result;
  }
  oldRender();
}

// ...
